const api = require("../../../ws_micro_shop/utils/api.js");
const app = getApp();
const { gatherAction = "", reportAction = "" } = require("../../../ws_micro_shop/utils/utils");
Page({
  data: {
    form: {
      name: '',
      mobile: '',
    },
    check: false,
    agree: 0,
    height: wx.systemInfo.windowHeight,
    classa: (app.tabbarindex === 0 ? false : true) || app.siteInfo.tabbar_type === '2',
    goods_list: [],
    show_no_data_tip: false,
    theme: app.themeData,
    abnorData: {
      imgSrc: '/common/static/images/abnor-08.png',
      text: '抱歉！没找到相关商品~',
    },
    is_show: false,
    condition: 0,
    isHidden: false,
    imgUrl: app.imgUrl,
  },
  onLoad(options) {
    let opts = options || {};
    this.setData({
      orgPrice: app.ws_micro_shop_globalData.orgPrice,
    })
    let that = this;
    let condition = Number(wx.getStorageSync('share_setting').share_condition);
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        currency: res.currency,
      });
    });
    app.loadShopSetting().then(res => {
      let { check_mobile, alias, privilege_show } = res;
      that.setData({
        check_mobile,
        alias,
        privilege_show
      });
      wx.setNavigationBarTitle({
        title: condition === 3 ? '购买成为' + alias : '申请成为' + alias,
      });
    });
    this.setData({
      opts,
      condition
    });
  },
  onShow() {
    let that = this;
    let user_info = wx.getStorageSync('user_info');
    let store = wx.getStorageSync('store');
    let share_setting = wx.getStorageSync('share_setting');
    wx.showLoading({
      title: '加载中',
      mask: true,
    });

    that.setData({
      user_info,
      store,
      share_setting,
    });
    app.ws_micro_shoprequest({
      url: api.share.check,
      method: 'POST',
      success: function (res) {
        if (res.code === 0) {
          user_info.is_distributor = res.data;
          user_info.level = res.level;
          wx.setStorageSync("user_info", user_info);
          Number(res.data) === 1 &&
            app.util.navigateTo(
              "/ws_micro_shop_seller/pages/share/index",
              {},
              true
            );
          that.setData({
            user_info,
            is_show: true,
            goods_list: res.goods_list,
            apply_columns: res.apply_columns,
            show_no_data_tip: (res.goods_list.length === 0),
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      },
    });
  },
  getFenxiao() {
    let { isHidden } = this.data;
    this.setData({
      isHidden: !isHidden,
    })
  },
  formSubmit(e) {
    let that = this;
    let { user_info, apply_columns, check_mobile, currency, agree } = that.data;
    that.data.form = e.detail.value;
    for (let item of apply_columns) {
      if (!that.data.form[item.key]) {
        wx.showToast({
          title: '请填写' + item.title + '!',
          icon: 'none',
        });
        return;
      }
      if (item.key === 'mobile') {
        if (Number(check_mobile) === 1) {
          if (currency === 'CNY') {
            let mobile = that.data.form[item.key];
            if (!app.util.phoneExp.mobile.test(mobile)) {
              wx.showToast({
                title: '手机号码格式不正确！',
                icon: 'none',
              });
              return;
            }
          } else {
            if (!app.util.phoneExp.canadaPhone.test(mobile)) {
              wx.showToast({
                title: '手机号码格式不正确！',
                icon: 'none',
              });
              return;
            }
          }
        }
      }
    }
    if (Number(agree) === 0) {
      wx.showToast({
        title: '请先阅读并确认分销申请协议！！',
        icon: 'none',
      });
      return;
    }
    let data = e.detail.value;
    
    wx.showLoading({
      title: '正在提交',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.share.join,
      method: 'POST',
      data: data,
      success: function (res) {
        if (res.code === 0) {
          user_info.is_distributor = 2;
          wx.setStorageSync('user_info', user_info);
          wx.redirectTo({
            url: "/ws_micro_shop_seller/pages/add-share/index"
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
          });
        }
      },
    });
  },
  agreement() {
    let share_setting = this.data.share_setting;
    wx.showModal({
      title: '分销协议',
      content: share_setting.agree || '暂无内容',
      showCancel: false,
      confirmText: '我已阅读',
      confirmColor: '#ff9500',
    });
  },
  agree() {
    let agree = Number(this.data.agree);
    if (agree === 0) {
      this.setData({
        check: true,
        agree: 1,
      });
    } else if (agree === 1) {
      this.setData({
        check: false,
        agree: 0,
      });
    }
  },
  onNavigate(e) {
    let param = e.currentTarget.dataset;
    app.ws_micro_shop_navigate(param);
  },
  gotoGoodsDetail(e) {
    gatherAction && gatherAction('gotoGoodsDetail', 'success', { goodsID: e.currentTarget.dataset.goodsId });
  },
});
